import json
from decimal import Decimal

from django.test import TestCase

# Create your tests here.
from CoreSubSystem.models import user

# CoreSubSystem子应用单元测试
class userTest(TestCase):

    def setUp(self) -> None:
        user.objects.create(
            id=1,
            user_name="Yiyang",
            user_real_name="陈王艺阳",
            user_password="123456789",
            user_wallet_balance=7777.77,
            user_phone_number="13569998888",
            user_annual_income=5000.00,
            user_expected_time=5,
            user_expected_return_rate=15.55,
            user_risk_ratio=36.78
        )

    # user Model模型类测试
    def test_user_model(self):
        print("user模型类测试开始")
        result = user.objects.get(id=1)
        self.assertEqual(result.user_name, "Yiyang")
        self.assertEqual(result.user_real_name, "陈王艺阳")
        self.assertEqual(result.user_password, "123456789")
        self.assertEqual(result.user_wallet_balance, Decimal('7777.77'))
        self.assertEqual(result.user_phone_number, "13569998888")
        self.assertEqual(result.user_annual_income, Decimal('5000.00'))
        self.assertEqual(result.user_expected_time, 5)
        self.assertEqual(result.user_expected_return_rate, Decimal('15.55'))
        self.assertEqual(result.user_risk_ratio, Decimal('36.78'))
        print("user模型类测试完成")

    # get_all_users方法测试
    def test_get_all_users_controller(self):
        print("get_all_users方法测试开始")
        result = self.client.get("/system/getAllUsers?user_id=1")
        print("Yiyang的用户对象信息如下：")
        print(json.loads(result.content)['data'])
        print("get_all_users方法测试完成")

    # login方法测试
    def test_login_controller(self):
        print("login方法测试开始")
        # 登录成功
        data = {'user_name': ['1'], 'user_password': ['123456789']}
        result = self.client.post("/system/login", data=data)
        allUserInfo = json.loads(json.loads(result.content)['data'])
        self.assertEqual(result.status_code, 200)
        print(allUserInfo)
        self.assertEqual(allUserInfo[0]['fields']['user_real_name'], "陈王艺阳")
        self.assertTrue(json.loads(result.content)['flag'])
        # 登录失败
        data = {'user_name': ['2'], 'user_password': ['123456789']}
        result = self.client.post("/system/login", data=data)
        self.assertFalse(json.loads(result.content)['flag'])
        print("login方法测试完成")

    def tearDown(self) -> None:
        print("------------------")